#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.web.security;


import guda.grape.mvc.spring.SpringBeanFactoryTool;
import guda.grape.mvc.uribox.URIBox;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by well on 16/6/21.
 */
public class DefaultAuthFilter implements Filter,InitializingBean {

    private static final Logger log = LoggerFactory.getLogger(DefaultAuthFilter.class);

    private DefaultAuthChecker bigvAuthChecker;

    private DefaultAuthChecker.Callback callback;


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    private String getServerName(HttpServletRequest httpServletRequest){
        if(httpServletRequest == null){
            return null;
        }
        return httpServletRequest.getServerName();
    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        if (request instanceof HttpServletRequest) {
            HttpServletRequest httpRequest = (HttpServletRequest) request;
            String requestURI = httpRequest.getRequestURI();
            String serverName = getServerName(httpRequest);
            if("/".equals(requestURI)){
                try {

                    if(serverName.indexOf("admin.liangmu.ren")>-1){
                        URIBox adminServer = SpringBeanFactoryTool.getBeanFactory().getBean("adminServer", URIBox.class);
                        ((HttpServletResponse)response).sendRedirect(adminServer+"/admin/login.htm");
                        return ;
                    }else if(serverName.indexOf("zx.liangmu.ren")>-1){
                        URIBox wapServer = SpringBeanFactoryTool.getBeanFactory().getBean("wapServer", URIBox.class);
                        ((HttpServletResponse)response).sendRedirect(wapServer+"/wap/login.htm");
                        return ;
                    }else{
                        ((HttpServletResponse)response).sendRedirect("/wap/login.htm");
                        return ;
                    }

                } catch (Exception e) {
                    log.error("",e);
                }

            }
            if(requestURI.startsWith("/wap/login.htm") && serverName.indexOf("admin.liangmu.ren")>-1){
                URIBox wapServer = SpringBeanFactoryTool.getBeanFactory().getBean("wapServer", URIBox.class);
                ((HttpServletResponse)response).sendRedirect(wapServer+"/wap/login.htm");
                return ;
            }
            if(requestURI.startsWith("/admin/login.htm") && serverName.indexOf("zx.liangmu.ren")>-1){
                URIBox adminServer = SpringBeanFactoryTool.getBeanFactory().getBean("adminServer", URIBox.class);
                ((HttpServletResponse)response).sendRedirect(adminServer+"/admin/login.htm");
                return ;
            }



            if (requestURI.startsWith("/help/")||requestURI.startsWith("/home/")
                    ||requestURI.startsWith("/admin/login.htm")
                    ||requestURI.startsWith("/wap/login.htm")
                    ||requestURI.startsWith("/login.htm")
                    ||"/".equals(requestURI)||requestURI.startsWith("/front/wx/")) {
                chain.doFilter(request, response);
            } else if (((HttpServletRequest) request).getRequestURI().endsWith(".do")){
                chain.doFilter(request, response);
            }else {
                boolean check = bigvAuthChecker.check((HttpServletRequest) request, (HttpServletResponse) response);
                if (check) {
                    chain.doFilter(request, response);
                }
            }
        }

    }

    @Override
    public void destroy() {

    }

    public void setCallback(DefaultAuthChecker.Callback callback) {
        this.callback = callback;
    }

    public void setPageAuthChecker(DefaultAuthChecker starkAuthChecker) {
        this.bigvAuthChecker = starkAuthChecker;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        if(bigvAuthChecker == null) {
            bigvAuthChecker = new DefaultAuthChecker();
        }
        if(callback != null) {
            bigvAuthChecker.setCallback(callback);
        }
    }
}
